// QuickBlox application settings
var QB = {
	appId : '4700',
	authKey : 'ZChfeyzePe9UZtH',
	authSecret : '9hk-bkU2QVt6Gvw'
}



// Script runs main() when when PhoneGap is fully loaded.
// http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html#deviceready
/*function init() {
	document.addEventListener("deviceready", main, true);
	$(document).ready(main);
}
*/
function main() {
	authApp(appHasToken, errorCallback);
}

function loadStreaming() {
	authApp(appHasToken, errorCallback);
}

// Authenticate specified QuickBlox application.
// Calls successCallback if finished successfully, and errorCallback if not.
function authApp(successCallback, errorCallback) {
	var s = getSignature(); // gets signature
	
	// See more documentation on the wiki -- http://wiki.quickblox.com/Authentication_and_Authorization#API_Session_Creation
	var url = 'https://api.quickblox.com/session';
	var data = 'app_id=' + QB.appId + 
			'&auth_key=' + QB.authKey + 
			'&nonce=' + s.nonce + 
			'&timestamp=' + s.timestamp + 
			'&signature=' + s.signature;
	
	//alert('[DEBUG] Authenticate specified application: POST ' + url + '?' + data);
	
	$.ajax({
	  type: 'POST',
	  url: url,
	  data: data,
	  success: successCallback,
	  error: errorCallback
	});
}

// Calls when QuickBlox application authorize.
function appHasToken(xml) {
	// Finds token in retrieved xml response.
	var token = $(xml).find('token').text();
	getAllRecords(token);
	
}

function errorCallback(jqXHR, textStatus, errorThrown) {
	// alert('Error: ' + jqXHR + ' | textStatus: ' + jqXHR.status + ' | errorThrown ' + errorThrown);
	if (jqXHR.status == 0) {
                showConfirm();
                window.location.href='index.html'	
			} else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');	
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
	
}



// Obtiene los Registros de QuickBlox de CustomObjects para Video y Twitter
function getAllRecords(token) {
	// See more documentation on the wiki -- http://wiki.quickblox.com/Users#Retrieve_API_Users_for_current_application
	var url = 'https://api.quickblox.com/data/Live.json'
	var data = 'limit=1&sort_desc=fecha';

    
	$.ajax({
           type: 'GET',
           url: url,
           data: data,
           headers: {
           "Content-Type":"application/json",
           "QuickBlox-REST-API-Version":"0.1.1",
           "QB-Token":token
           },
           	success: function(response) {
           		$(response.items).each(function(index, current){
		        	var youtube = '<iframe id="youtube-frame" allowfullscreen="" frameborder="1" height="100%" src="' + current.url + '?rel=0&showinfo=0" width="100%"></iframe>'
		            var image   =  '<img src = "'  + current.url + '" height="100%" />  '
		            //$('#titulo').append(current.titulo);
		            //$('#descripcion').append(current.desc);
		            //$('#descripcion-vertical').append(current.desc);
		            if(current.tipo == 'h' ){
		            	$('#videochat-youtube').append(youtube);
					} else {
						$('#videochat-youtube').append(image);
					}

					if(current.timeline1 != null && $.trim(current.timeline1).length > 0){
				    	$('#tl1Title').append('<h2>' + current.timeline1titulo+ '</h2>');
					    $.getJSON("http://www.marmolesypiedras.com/twit/tweet/tweets-vivo.php?tweet="+current.timeline1, function(tweets){
			            	if (tweets.statuses !== undefined){
								tweets = tweets.statuses;
							}
			                $.each(tweets, function(i, tweet) {
			                                //let's check to make sure we actually have a tweet
			                                //alert('tweet.text ' + tweet.text);
			                                if (tweet.text !== undefined) {

			                                    var username = tweet.user.screen_name;
			                                    //alert('username ' + username);
			                                    //clean things up a bit
			                                    var status = tweet.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			                                        return '<a href="'+url+'">'+url+'</a>';
			                                    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			                                        return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			                                    });

			                                    var statusHTML = '<li class="trino"><a href="https://twitter.com/'+  username  + ' " class="headerTrino"> <img src=" ' + tweet.user.profile_image_url +  ' "> <h3>'+  username  + '</h3></a> <p class="cuerpoTrino">'+status+' </p>';
                              					$('#tl1').append(statusHTML);
			                                }
			                            });
									
			                    });
							} else {
								$(this).remove();
							}
						
		
					//timeline 2
					if(current.timeline2 != null && $.trim(current.timeline2).length > 0){
						$('#tl2Title').append('<h2>' + current.timeline2titulo + '</h2>');
			            $.getJSON("http://www.marmolesypiedras.com/twit/tweet/tweets-vivo.php?tweet="+current.timeline2, function(tweets){
	                    	if (tweets.statuses !== undefined){
								tweets = tweets.statuses;
							}
	                    	$.each(tweets, function(i, tweet) {
	                        	//let's check to make sure we actually have a tweet
	                            if (tweet.text !== undefined) {
	                            	var username = tweet.user.screen_name;
	                                //clean things up a bit
	                                var status = tweet.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
	                                        return '<a href="'+url+'">'+url+'</a>';
	                                }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
	                                        return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	                                });
	                                var statusHTML = '<li class="trino"><a href="https://twitter.com/'+  username  + ' " class="headerTrino"> <img src=" ' + tweet.user.profile_image_url +  ' "> <h3>'+  username  + '</h3></a> <p class="cuerpoTrino">'+status+' </p>';
                              		$('#tl2').append(statusHTML);
	                            }
	                        });
	                    });	
					} else {
						$('#tl2').remove();
					}

					//timeline 3
			        if(current.timeline3 != null && $.trim(current.timeline3).length > 0){
						$('#tl3Title').append('<h2>' +  current.timeline3titulo + '</h2>');
			            $.getJSON("http://www.marmolesypiedras.com/twit/tweet/tweets-vivo.php?tweet="+current.timeline3, function(tweets){
	                    	if (tweets.statuses !== undefined){
								tweets = tweets.statuses;
							}
	                    	$.each(tweets, function(i, tweet) {

	                                //let's check to make sure we actually have a tweet
	                                if (tweet.text !== undefined) {
	                                    var username = tweet.user.screen_name;
	                                    //clean things up a bit
	                                    var status = tweet.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
	                                        return '<a href="'+url+'">'+url+'</a>';
	                                    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
	                                        return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	                                    });
	                                    var statusHTML = '<li class="trino"><a href="https://twitter.com/'+  username  + ' " class="headerTrino"> <img src=" ' + tweet.user.profile_image_url +  ' "> <h3>'+  username  + '</h3></a> <p class="cuerpoTrino">'+status+' </p>';
                              			$('#tl3').append(statusHTML);  
	                                }
	                            });
	                    });	
					} else {
						$('#tl3').remove();
					}
					
					var live = new Boolean(current.streaming);
	           		if(live == true ){
						$('#inferiorLive').attr('src','img/botonLIFEon@2x.png');
	           		}	
				});
				
	        
           },
           error: errorCallback
           });
		
}
           

   

// Authenticates user.
function authUser(token) {
	var login = $('#login').val();
	var password = $('#password').val();
	var url = 'http://api.quickblox.com/login.json'
	var data = 'login='+login+'&password='+password;
	
	//alert('[DEBUG] Authenticate existing user: POST ' + url + '?' + data);
	
	$.ajax({
		type: 'POST',
		headers: {
        	"Content-Type":"application/json",
        	"QuickBlox-REST-API-Version":"0.1.1",
        	"QB-Token":token
    	},
		url: url,
		data: data,
		success: function(response) {
			alert('[DEBUG] User has been successfully authenticated. Server response:');
			alert('[DEBUG] User has been successfully authenticated, user id = ' + response.id);
			alert(response);
		},
		error: errorCallback
	});
}



// Gets signature. Signature uses for application authentication.
function getSignature() {
	var nonce = Math.floor(Math.random() * 1000); // Gets random number (0;1000)
	var timestamp = Math.round((new Date()).getTime() / 1000); // Gets unix timestamp (http://en.wikipedia.org/wiki/Unix_time) 

	// Creating message where parameters are sorted by alphabetical order.
	var message = 'app_id=' + QB.appId + '&auth_key=' + QB.authKey + '&nonce=' + nonce + '&timestamp=' + timestamp;
	var secret = QB.authSecret;
	// Encrypting message with secret key from QuickBlox application parameters.
	var hmac = Crypto.HMAC(Crypto.SHA1, message, secret);
	
	var signatureObj = {
		nonce 		: nonce,
		timestamp 	: timestamp,
		signature 	: hmac
	};
	
	return signatureObj; 
}


// By Chris Coyier & tweaked by Mathias Bynens

$(function() {

	// Find all YouTube videos
	var $allVideos = $("iframe[src^='http://www.youtube.com']"),

	    // The element that is fluid width
	    $fluidEl = $("videochat-youtube");

	// Figure out and save aspect ratio for each video
	$allVideos.each(function() {

		$(this)
			.data('aspectRatio', this.height / this.width)
			
			// and remove the hard coded width/height
			.removeAttr('height')
			.removeAttr('width');

	});

	// When the window is resized
	// (You'll probably want to debounce this)
	$(window).resize(function() {

		var newWidth = $fluidEl.width();
		
		// Resize all videos according to their own aspect ratio
		$allVideos.each(function() {

			var $el = $(this);
			$el
				.width(newWidth)
				.height(newWidth * $el.data('aspectRatio'));

		});

	// Kick off one resize to fix all videos on page load
	}).resize();

});

/* Crypto-JS algorithm from http://crypto-js.googlecode.com/files/2.3.0-crypto-sha1-hmac.js */

/*
 * Crypto-JS v2.3.0
 * http://code.google.com/p/crypto-js/
 * Copyright (c) 2011, Jeff Mott. All rights reserved.
 * http://code.google.com/p/crypto-js/wiki/License
 */
if(typeof Crypto=="undefined"||!Crypto.util)(function(){var i=window.Crypto={},n=i.util={rotl:function(a,c){return a<<c|a>>>32-c},rotr:function(a,c){return a<<32-c|a>>>c},endian:function(a){if(a.constructor==Number)return n.rotl(a,8)&16711935|n.rotl(a,24)&4278255360;for(var c=0;c<a.length;c++)a[c]=n.endian(a[c]);return a},randomBytes:function(a){for(var c=[];a>0;a--)c.push(Math.floor(Math.random()*256));return c},bytesToWords:function(a){for(var c=[],b=0,d=0;b<a.length;b++,d+=8)c[d>>>5]|=a[b]<<24-
d%32;return c},wordsToBytes:function(a){for(var c=[],b=0;b<a.length*32;b+=8)c.push(a[b>>>5]>>>24-b%32&255);return c},bytesToHex:function(a){for(var c=[],b=0;b<a.length;b++){c.push((a[b]>>>4).toString(16));c.push((a[b]&15).toString(16))}return c.join("")},hexToBytes:function(a){for(var c=[],b=0;b<a.length;b+=2)c.push(parseInt(a.substr(b,2),16));return c},bytesToBase64:function(a){if(typeof btoa=="function")return btoa(j.bytesToString(a));for(var c=[],b=0;b<a.length;b+=3)for(var d=a[b]<<16|a[b+1]<<
8|a[b+2],e=0;e<4;e++)b*8+e*6<=a.length*8?c.push("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(d>>>6*(3-e)&63)):c.push("=");return c.join("")},base64ToBytes:function(a){if(typeof atob=="function")return j.stringToBytes(atob(a));a=a.replace(/[^A-Z0-9+\/]/ig,"");for(var c=[],b=0,d=0;b<a.length;d=++b%4)d!=0&&c.push(("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(a.charAt(b-1))&Math.pow(2,-2*d+8)-1)<<d*2|"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(a.charAt(b))>>>
6-d*2);return c}};i=i.charenc={};i.UTF8={stringToBytes:function(a){return j.stringToBytes(unescape(encodeURIComponent(a)))},bytesToString:function(a){return decodeURIComponent(escape(j.bytesToString(a)))}};var j=i.Binary={stringToBytes:function(a){for(var c=[],b=0;b<a.length;b++)c.push(a.charCodeAt(b)&255);return c},bytesToString:function(a){for(var c=[],b=0;b<a.length;b++)c.push(String.fromCharCode(a[b]));return c.join("")}}})();
(function(){var i=Crypto,n=i.util,j=i.charenc,a=j.UTF8,c=j.Binary,b=i.SHA1=function(d,e){var f=n.wordsToBytes(b._sha1(d));return e&&e.asBytes?f:e&&e.asString?c.bytesToString(f):n.bytesToHex(f)};b._sha1=function(d){if(d.constructor==String)d=a.stringToBytes(d);var e=n.bytesToWords(d),f=d.length*8;d=[];var k=1732584193,g=-271733879,l=-1732584194,m=271733878,o=-1009589776;e[f>>5]|=128<<24-f%32;e[(f+64>>>9<<4)+15]=f;for(f=0;f<e.length;f+=16){for(var q=k,r=g,s=l,t=m,u=o,h=0;h<80;h++){if(h<16)d[h]=e[f+
h];else{var p=d[h-3]^d[h-8]^d[h-14]^d[h-16];d[h]=p<<1|p>>>31}p=(k<<5|k>>>27)+o+(d[h]>>>0)+(h<20?(g&l|~g&m)+1518500249:h<40?(g^l^m)+1859775393:h<60?(g&l|g&m|l&m)-1894007588:(g^l^m)-899497514);o=m;m=l;l=g<<30|g>>>2;g=k;k=p}k+=q;g+=r;l+=s;m+=t;o+=u}return[k,g,l,m,o]};b._blocksize=16;b._digestsize=20})();
(function(){var i=Crypto,n=i.util,j=i.charenc,a=j.UTF8,c=j.Binary;i.HMAC=function(b,d,e,f){if(d.constructor==String)d=a.stringToBytes(d);if(e.constructor==String)e=a.stringToBytes(e);if(e.length>b._blocksize*4)e=b(e,{asBytes:true});var k=e.slice(0);e=e.slice(0);for(var g=0;g<b._blocksize*4;g++){k[g]^=92;e[g]^=54}b=b(k.concat(b(e.concat(d),{asBytes:true})),{asBytes:true});return f&&f.asBytes?b:f&&f.asString?c.bytesToString(b):n.bytesToHex(b)}})();